import java.io.File;
import java.io.IOException;

import drive.DriveLocations;

import exception.URLNotFoundException;

import name.raynal.tool.SebUrlFile;

public class RendezVousXMain {

	private static final String TEMP_LOCATION = DriveLocations.CLYDE + "/Documents/RDVX";
	private static final String SERVER_ADRESS = "http://rendezvousavecmrx.free.fr";
	private static boolean isDownloadStarted = false;

	public static void main(String[] args) throws IOException {
		System.out.println("START");
		new File(TEMP_LOCATION).mkdirs();

		SebUrlFile mainPage = new SebUrlFile(TEMP_LOCATION + "/index.htm");
		try {
			mainPage.copyURLcontent("http://rendezvousavecmrx.free.fr/page/liste_pc.php?tri=3");
		} catch (URLNotFoundException e) {
			e.printStackTrace();
		}
		String content = mainPage.getContent();
		
		int count = 0;
		while (content.contains("mr_x")) {
			int index = content.indexOf("mr_x");
			String mp3Name = content.substring(index, index + 19);
			
			if (!isDownloadStarted) {
				isDownloadStarted  = "mr_x_2010_04_24.mp3".equals(mp3Name);
				// refaire mr_x_2000_04_01 et mr_x_2000_19_09
			}
				// mr_x_2009_05_09.mp3
			
			if (isDownloadStarted) {
				System.out.println(mp3Name + "...");
				SebUrlFile mp3RemoteFile = new SebUrlFile(TEMP_LOCATION + "/"
						+ mp3Name);
				String mp3Address = SERVER_ADRESS + "/audio/" + mp3Name;
				try {
					mp3RemoteFile.copyURLcontent(mp3Address);
				} catch (URLNotFoundException e) {
					e.printStackTrace();
				}
				System.out.print(" downloaded.");
			}
			content = content.substring(index + 19);
			count++;
		}
		System.out.println("Count = " + count);
		// mr_x_2011_03_19.mp3

		Runtime.getRuntime().exec("explorer \"" + TEMP_LOCATION + "\"");
		System.out.println("DONE");
	}
}
